import Vue from 'vue'
import api from '../api'
Vue.use(api)
import '../utils/rem'

Vue.prototype.$imgToBase64 = img => {
  let dataURL = ''
  let canvas = document.createElement("canvas");
  let ctx = canvas.getContext("2d");
  let imgNode = new Image();
  imgNode.src = img
  imgNode.setAttribute("crossOrigin", "Anonymous");
  return new Promise(resolve => {
    imgNode.addEventListener("load", function () {
      canvas.width = imgNode.width;
      canvas.height = imgNode.height;
      ctx.drawImage(imgNode, 0, 0, imgNode.width, imgNode.height);
      dataURL = canvas.toDataURL("image/png");
      resolve('image://' + dataURL)
    })
  })
};
// 字节单位换算
Vue.prototype.$byteConvert = function (bytes) {
  if (isNaN(bytes)) {
    return "";
  }
  var symbols = ["bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"];
  var exp = Math.floor(Math.log(bytes) / Math.log(2));
  if (exp < 1) {
    exp = 0;
  }
  var i = Math.floor(exp / 10);
  bytes = bytes / Math.pow(2, 10 * i);

  if (bytes.toString().length > bytes.toFixed(2).toString().length) {
    bytes = bytes.toFixed(2);
  }
  return bytes + " " + symbols[i];
};